package com.windoent.yugioh.ui.activity.setting;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/12 16:26
 * desc   :
 */
public class SettingChangePhoneActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private EditText mEtOldPhone;
    private EditText mEtNewPhone;
    private Button mBtnGetCode;


    @Override
    public int getLayout() {
        return R.layout.activity_setting_change_phone;
    }

    @Override
    public void initView() {
        super.initView();

        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mEtOldPhone = (EditText) findViewById(R.id.et_old_phone);
        mEtNewPhone = (EditText) findViewById(R.id.et_new_phone);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);
    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //下一步
                String phoneNumber = mEtNewPhone.getText().toString().trim();
                String phoneNumberOld = mEtOldPhone.getText().toString().trim();
                if (BaseMethod.isNull(phoneNumberOld)) {
                    ToastUtils.showText(getString(R.string.string_input_old_phone_number_null));
                    return;
                }
                if (BaseMethod.isNull(phoneNumber)) {
                    ToastUtils.showText(getString(R.string.string_input_new_phone_number_null));
                    return;
                }
                resetPhone(phoneNumberOld, phoneNumber);
            }
        });
    }

    //修改手机号
    private void resetPhone(String phoneNumberOld, String phoneNumber) {
        showLoadingDialog("");
        PhoneLive.resetPhone(getUserToken(), phoneNumberOld, phoneNumber, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean json = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(json.getMsg());
                            getCellPhoneCode(phoneNumber);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //发送验证码
    private void getCellPhoneCode(String phone) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(phone, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                            Intent intent = new Intent(mContext, SettingChangePhoneVerifyCodeActivity.class);
                            intent.putExtra("phoneNumber", phone);
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "设置修改手机号界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "设置修改手机号界面");
    }

}
